<template>
  <div class="singer" ref="singer">
    <list-view :data="singers" @select="selectSinger" ref="list"></list-view>
    <transition name="slide">
      <router-view></router-view>
    </transition>
  </div>
</template>

<script type="text/ecmascript-6">
import { getSingerList } from 'api/singer'
import { ERR_OK } from 'api/config'
import Singer from 'common/js/singer'
import ListView from 'base/listview/listview'
import { mapMutations } from 'vuex'
import { playlistMixin } from 'common/js/mixin'

const HOT_NAME = '热门'
const HOT_SINGER_LEN = 10

export default {
  mixins: [playlistMixin],
  data () {
    return {
      singers: []
    }
  },
  created () {
    this._getSingerList()
  },
  methods: {
    _getSingerList () {
      try {
        getSingerList()
          .then(result => {
            console.log(result)
            if (result.code === ERR_OK) {
              this.singers = this._normalizeSinger(result.data.list)
            }
          })
          .catch(error => {
            console.error(error)
          })
      } catch (accident) {
        console.log(accident)
      }
    },
    _normalizeSinger (list) {
      // 热门数据
      let map = {
        hot: {
          title: HOT_NAME,
          items: []
        }
      }

      list.forEach((item, index) => {
        // 前十条数据为热门数据
        if (index < HOT_SINGER_LEN) {
          // 数据贼乱我们进行数据过滤
          map.hot.items.push(
            new Singer({
              id: item.Fsinger_mid,
              name: item.Fsinger_name
            })
          )
        }

        // 声明 key 判断有没有这个 key 所对应的值
        const key = item.Findex
        // 如果没有我们就创建这个 key 所对应的值
        if (!map[key]) {
          map[key] = {
            title: key,
            items: []
          }
        }

        map[key].items.push(
          new Singer({
            id: item.Fsinger_mid,
            name: item.Fsinger_name
          })
        )

        /**
         * 这里的主要逻辑就是 首先拿出 item 中的 Findex 作为 key,判断 map 对象里面有没有对应的 key
         * 没有的话 就使用 key 作为 map对象的键
         * 有的话就直接在 map 对象对应的键添加对应的值
         */
      })

      // 为了得到有序列表我们处理map
      let hot = []
      let ret = []
      for (let key in map) {
        let value = map[key]
        if (value.title.match(/[a-zA-Z]/)) {
          ret.push(value)
        } else if (value.title === HOT_NAME) {
          hot.push(value)
        }
      }

      // 排序
      ret.sort((a, b) => {
        // console.log(a.title.charCodeAt(0))
        return a.title.charCodeAt(0) - b.title.charCodeAt(0)
      })
      // console.log(hot.concat(ret))
      return hot.concat(ret)
    },
    selectSinger (item) {
      // console.log(item)
      this.$router.push({ name: 'SingerDetail', params: { id: item.id } })
      this.setSinger(item)
    },
    handlePlaylist (playlist) {
      const bottom = playlist.length ? '60px' : ''
      this.$refs.singer.style.bottom = bottom
      this.$refs.list.refresh()
    },
    ...mapMutations({
      setSinger: 'SET_SINGER'
    })
  },
  components: { ListView }
}
</script>

<style lang="stylus" scoped rel="stylesheet/stylus">
  @import '~common/stylus/variable';

  .singer {
    position: fixed;
    top: 88px;
    bottom: 0;
    width: 100%;
    // overflow: hidden;

    .slide-enter-active, .slide-leave-active {
      transition: all 0.4s;
    }

    .slide-enter, .slide-leave-to {
      transform: translate3d(100%, 0, 0);
    }
  }
</style>
